/*
* $Id: CategoryItem.java 250 2011-01-17 21:36:24Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2011 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.ui;

import javax.microedition.lcdui.*;
import java.io.IOException;
import apiary.Apiary;
import apiary.common.StringFunction;
import java.util.Vector;

public class CategoryItem extends CustomItem
{
// item properties
    private Image img;
    private String nameCategory;
    private String id;
    private boolean isActive;
    private int colorItem;
    private int colorActive;
    private ItemEventListener eventListener;
    private String state;

// custom properties
    private Font font;
    private Vector printName;
    
    public CategoryItem(String id_, String name, String imagePath){
        super("");
        isActive = false;
        nameCategory = name;
        state = "";
        setImage(imagePath);
        font = Font.getDefaultFont();
        id = id_;
        eventListener = null;
        printName = new Vector();
        GetFormatedText();
    }

/*******************************************************************/
/*                 setters and getters                             */
/*******************************************************************/
    public final void setImage(String imagePath){
        try {
            img = Image.createImage(imagePath);
            repaint();
        }
        catch (IOException ioe) {
            img = null;
        }
    }

    public final void setState(String st){
        state = st;
    }
    public final String getState(){
        return state;
    }

    public final void setColor(int r, int g, int b){
        colorItem = r<<16|g<<8|b;
        colorActive = colorItem;
    }

    public final void setColorActive(int r, int g, int b){
        colorActive = r<<16|g<<8|b;
    }

    public void setItemEventListener(ItemEventListener listener){
        eventListener = listener;
    }

    public void setActive(boolean active){
        if(isActive != active){
            isActive = active;
            notifyStateChanged();
            repaint();
        }
    }
    public boolean getActive(){
        return isActive;
    }

    public void setId(String t){
        id = t;
    }
    public String getId(){
        return id;
    }

    public String getName() {
        return nameCategory;
    }

    public void setName(String name) {
        nameCategory = name;
        GetFormatedText();
    }

/*******************************************************************/
/*                       custom item function                      */
/*******************************************************************/
    protected int getMinContentHeight(){
        int heightString = font.getHeight();
        if(printName.size() > 1)
            heightString = font.getHeight() * printName.size();
        int heightImage = 0;
        if(img != null)
            heightImage = img.getHeight();
        if(heightImage < heightString)
            return heightString;
        else
            return heightImage;
    }
    protected int getMinContentWidth(){
        return Apiary.getInstance().getDisplay().getWidth();
    }
    protected int getPrefContentHeight(int width){
        return getMinContentHeight();
    }
    protected int getPrefContentWidth(int height){
       return getMinContentWidth();
    }
    protected void paint(Graphics g, int w, int h){
        int textPosX = 0;
        int color = g.getColor();
        if(isActive)
            g.setColor(colorActive);
        else
            g.setColor(colorItem);
        g.fillRect(textPosX, 0, w, h);
        g.setColor(color);
        if(img != null){
            g.drawImage(img, 0, h/2, Graphics.VCENTER | Graphics.LEFT);
            textPosX  = img.getWidth();
        }
        for(int i = 0, y = 0; i < printName.size(); i++, y += font.getHeight())
            g.drawString((String)printName.elementAt(i), (w + textPosX)/2, y, Graphics.HCENTER | Graphics.TOP);
    }

    protected  boolean traverse(int dir, int viewportWidth, int viewportHeight, int[] visRect_inout){
        if(Apiary.getInstance().getDisplay().isShown()) {
            setActive(true);
            return false;
        }
        return true;
    }

    protected void traverseOut(){
        if(Apiary.getInstance().getDisplay().isShown())
            setActive(false);
        super.traverseOut();
    }
    protected void keyPressed(int keyCode) {
        if(keyCode == -5)
            eventListener.itemNotifyEvent(this, ItemEventListener.PRESS);
    }
    protected void pointerPressed(int x, int y) {
        if(isActive && eventListener != null)
            eventListener.itemNotifyEvent(this, ItemEventListener.PRESS);
    }

    protected void sizeChanged(int w, int h) {
        GetFormatedText();
    }
/*******************************************************************/
/*                       user function                             */
/*******************************************************************/
    private void GetFormatedText(){
        printName.removeAllElements();
        int widthScreen = Apiary.getInstance().getDisplay().getWidth();
        int widthImage = 0;
        if(img != null)
            widthImage = img.getWidth();
        int widthString = font.stringWidth(nameCategory);
        int widthSpace = font.stringWidth(" ");
        String name = nameCategory;
        if(widthScreen - widthImage < widthString){
            Vector words = StringFunction.GetWords(nameCategory);
            widthString = 0;
            name = "";
            for(int i = 0; i < words.size(); i++){
                String word = (String)words.elementAt(i);
                int widthWord = font.stringWidth(word);
                if(name.length() != 0){
                    name = name.concat(" ");
                    widthString += widthSpace;
                }
                if(widthScreen - widthImage < widthString + widthWord){
                    widthString = 0;
                    if(name.length() == 0){
                        printName.addElement(word);
                        continue;
                     } else {
                         printName.addElement(name);
                         name = "";
                     }
                }
                name = name.concat(word);
                widthString += widthWord;
            }
        }
        if(name.length() != 0)
            printName.addElement(name);
    }
}
